package com.zhongshengnetwork.rightbe.letter.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.Constant.Constant;
import com.zhongshengnetwork.rightbe.Constant.LetterConstant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.activity.MyWebActivity;
import com.zhongshengnetwork.rightbe.common.customview.CustomDialog;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.SXStateModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;
import com.zhongshengnetwork.rightbe.lang.activity.LangPublishActivity;
import com.zhongshengnetwork.rightbe.lang.viewmodel.LetterViewModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LetterSettingActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener{

    private List<String> list=new ArrayList<String>();
    private List<String> groupkey=new ArrayList<String>();
    private ListView listview;
    private TopBarView topbar;
    private boolean state = false;
    private int type = LetterConstant.sx_all_open_type;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_letter_setting);
        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);

        listview=(ListView) findViewById(R.id.letter_setting_listview);
        initData();
        adapter=new MyAdapter();
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("TAG","position="+position);
                if (position == 1){
                    if (state == true){
                        settingSX(0);
                    }else {
                        openSX();
                    }
                }else {
                    if (state == true){
                        if (position == 2){
                           settingSX(LetterConstant.sx_all_open_type);
                        }else if (position == 3){
                           settingSX(LetterConstant.sx_fan_open_type);
                        }else if (position == 4){
                           settingSX(LetterConstant.sx_care_open_type);
                        }else if (position == 6){
                            //黑名单
                            Intent intent = new Intent(LetterSettingActivity.this, BlackPeopleActivity.class);
                            startActivity(intent);
                        }
                    }else {
                        if (position == 3){
                            //黑名单
                            Intent intent = new Intent(LetterSettingActivity.this, BlackPeopleActivity.class);
                            startActivity(intent);
                        }
                    }
                }
            }
        });
        getData();
    }

    public void initData(){

        state = SPManageUtils.getInstance(CustomApplication.mContext).getSPBoolean(LetterConstant.SXStateKey, false);
        type = SPManageUtils.getInstance(CustomApplication.mContext).getSPInt(LetterConstant.SXTypeKey, LetterConstant.sx_all_open_type);

        groupkey.add("A组");
        List<String> aList = new ArrayList<String>();
        aList.add("开通私信");
        if (state == true){
            aList.add("对所有人开放");
            aList.add("对粉丝开放");
            aList.add("对我关注的人开放");
        }
        list.add("A组");
        list.addAll(aList);

        groupkey.add("B组");
        List<String> bList = new ArrayList<String>();
        bList.add("黑名单");
        list.add("B组");
        list.addAll(bList);

    }

    private void getData(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                final HashMap<String, String> map = new HashMap<String, String>();
                if (CommonUtils.isLogin() == true){
                    map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
                }
                String url = "miniapp/letter/"+"langappid"+"/state.do";
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        Log.e("TAG","私信状态："+data);
                        final CommonModel commonModel = GsonTools.getCommonModel(data);
                        if (commonModel.getFlag().equals("1")){
                            SXStateModel m = GsonTools.getSXStateModel(data);
                            if (m != null){
                                SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(LetterConstant.SXStateKey, m.isState());
                                SPManageUtils.getInstance(CustomApplication.mContext).putSPInt(LetterConstant.SXTypeKey,m.getType());
                                state = m.isState();
                                type = m.getType();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Log.e("TAG","更新私信状态");
                                        groupkey.clear();
                                        list.clear();
                                        groupkey.add("A组");
                                        List<String> aList = new ArrayList<String>();
                                        aList.add("开通私信");
                                        if (state == true){
                                            aList.add("对所有人开放");
                                            aList.add("对粉丝开放");
                                            aList.add("对我关注的人开放");
                                        }
                                        list.add("A组");
                                        list.addAll(aList);

                                        groupkey.add("B组");
                                        List<String> bList = new ArrayList<String>();
                                        bList.add("黑名单");
                                        list.add("B组");
                                        list.addAll(bList);
                                        adapter.notifyDataSetChanged();
                                    }
                                });
                            }
                        }else {
                            CustomApplication.showTip(commonModel, LetterSettingActivity.this);
                        }
                    }

                    @Override
                    public void onError(String msg) {

                    }
                });
            }
        }.start();
    }

    private void openSX(){
        final HashMap<String, String> map = new HashMap<String, String>();
        if (CommonUtils.isLogin() == true){
            map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        }
        String url = "miniapp/letter/"+"langappid"+"/open.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
                CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    SXStateModel m = GsonTools.getSXStateModel(data);
                    if (m != null){
                        SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(LetterConstant.SXStateKey, m.isState());
                        SPManageUtils.getInstance(CustomApplication.mContext).putSPInt(LetterConstant.SXTypeKey,m.getType());
                        state = m.isState();
                        type = m.getType();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.e("TAG","更新私信状态");
                                groupkey.clear();
                                list.clear();
                                groupkey.add("A组");
                                List<String> aList = new ArrayList<String>();
                                aList.add("开通私信");
                                if (state == true){
                                    aList.add("对所有人开放");
                                    aList.add("对粉丝开放");
                                    aList.add("对我关注的人开放");
                                }
                                list.add("A组");
                                list.addAll(aList);

                                groupkey.add("B组");
                                List<String> bList = new ArrayList<String>();
                                bList.add("黑名单");
                                list.add("B组");
                                list.addAll(bList);
                                adapter.notifyDataSetChanged();
                            }
                        });
                    }
                }else {

                    if (commonModel.getCode().equals(Constant.UserReturnCode.CODE_10011)) {//登录过期
                        CustomApplication.showTip(commonModel, LetterSettingActivity.this);
                        return;
                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try{
                                CustomDialog dialog = null;
                                final CustomDialog.Builder builder = new CustomDialog.Builder(LetterSettingActivity.this);
                                builder.setTitle("温馨提示");
                                builder.setMessage(commonModel.getMsg());
                                builder.setPositiveButton("马上了解", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        Intent wIntent = new Intent(LetterSettingActivity.this, MyWebActivity.class);
                                        wIntent.putExtra(APIKey.urlKey, Constant.HOST+"api/system/note.do");
                                        startActivity(wIntent);
                                    }
                                });
                                builder.setNegativeButton("知道了", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                                dialog = builder.create();
                                dialog.show();
                            }catch (Exception e){
                                LangPublishActivity.uploadError("开通私信报错："+e.getLocalizedMessage());
                                ToastUtil.show(LetterSettingActivity.this, commonModel.getMsg());
                            }
                        }
                    });

                }
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    private void settingSX(int setType){
        final HashMap<String, String> map = new HashMap<String, String>();
        if (CommonUtils.isLogin() == true){
            map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
        }
        map.put("type", setType+"");
        String url = "miniapp/letter/"+"langappid"+"/settings.do";
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
                CommonModel commonModel = GsonTools.getCommonModel(data);
                if (commonModel.getFlag().equals("1")){
                    SXStateModel m = GsonTools.getSXStateModel(data);
                    if (m != null){
                        SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(LetterConstant.SXStateKey, m.isState());
                        SPManageUtils.getInstance(CustomApplication.mContext).putSPInt(LetterConstant.SXTypeKey,m.getType());
                        state = m.isState();
                        type = m.getType();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.e("TAG","更新私信状态");
                                groupkey.clear();
                                list.clear();
                                groupkey.add("A组");
                                List<String> aList = new ArrayList<String>();
                                aList.add("开通私信");
                                if (state == true){
                                    aList.add("对所有人开放");
                                    aList.add("对粉丝开放");
                                    aList.add("对我关注的人开放");
                                }
                                list.add("A组");
                                list.addAll(aList);

                                groupkey.add("B组");
                                List<String> bList = new ArrayList<String>();
                                bList.add("黑名单");
                                list.add("B组");
                                list.addAll(bList);
                                adapter.notifyDataSetChanged();
                            }
                        });
                    }
                }else {
                    if (commonModel.getCode().equals(Constant.UserReturnCode.CODE_10011)) {//登录过期
                        CustomApplication.showTip(commonModel, LetterSettingActivity.this);
                        return;
                    }
                }
            }

            @Override
            public void onError(String msg) {

            }
        });
    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        //Toast.makeText(LoginActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (state == false){
                return 4;
            }
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            if(groupkey.contains(getItem(position))){
                return false;
            }
            return super.isEnabled(position);
        }

        /**
         * @param position
         * @param convertView
         * @param parent
         * @return
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View view=convertView;
            if(groupkey.contains(getItem(position))){
                view= LayoutInflater.from(CustomApplication.mContext).inflate(R.layout.my_list_heaer, null);
                TextView text = (TextView) view.findViewById(R.id.my_list_header_text);
                text.setText("");
            }else{
                view=LayoutInflater.from(CustomApplication.mContext).inflate(R.layout.new_message_tip_listview_item, null);
                TextView text=(TextView) view.findViewById(R.id.new_message_tip_text);
                ImageView mSwitch = (ImageView) view.findViewById(R.id.new_message_tip_switch);
                ImageView new_message_choose = (ImageView) view.findViewById(R.id.new_message_choose);
                ImageView new_message_next = (ImageView) view.findViewById(R.id.new_message_next);
                text.setText((CharSequence) getItem(position));
                text.setTag(position);
                if (position == 1){
                    mSwitch.setVisibility(View.VISIBLE);
                    new_message_choose.setVisibility(View.INVISIBLE);
                    new_message_next.setVisibility(View.INVISIBLE);
                    if (state == true){
                        mSwitch.setImageResource(R.drawable.ios7_switch_on);
                    }else {
                        mSwitch.setImageResource(R.drawable.ios7_switch_off);
                    }
                }else {
                    if (state == true){
                        if (position == 2){
                            if (type == LetterConstant.sx_all_open_type){
                                mSwitch.setVisibility(View.INVISIBLE);
                                new_message_choose.setVisibility(View.VISIBLE);
                                new_message_next.setVisibility(View.INVISIBLE);
                            }else {
                                mSwitch.setVisibility(View.INVISIBLE);
                                new_message_choose.setVisibility(View.INVISIBLE);
                                new_message_next.setVisibility(View.INVISIBLE);
                            }
                        }else if (position == 3){
                            if (type == LetterConstant.sx_fan_open_type){
                                mSwitch.setVisibility(View.INVISIBLE);
                                new_message_choose.setVisibility(View.VISIBLE);
                                new_message_next.setVisibility(View.INVISIBLE);
                            }else {
                                mSwitch.setVisibility(View.INVISIBLE);
                                new_message_choose.setVisibility(View.INVISIBLE);
                                new_message_next.setVisibility(View.INVISIBLE);
                            }
                        }else if (position == 4){
                            if (type == LetterConstant.sx_care_open_type){
                                mSwitch.setVisibility(View.INVISIBLE);
                                new_message_choose.setVisibility(View.VISIBLE);
                                new_message_next.setVisibility(View.INVISIBLE);
                            }else {
                                mSwitch.setVisibility(View.INVISIBLE);
                                new_message_choose.setVisibility(View.INVISIBLE);
                                new_message_next.setVisibility(View.INVISIBLE);
                            }
                        }else if (position == 6){
                            mSwitch.setVisibility(View.INVISIBLE);
                            new_message_choose.setVisibility(View.INVISIBLE);
                            new_message_next.setVisibility(View.VISIBLE);
                        }
                    }else {
                        mSwitch.setVisibility(View.INVISIBLE);
                        new_message_choose.setVisibility(View.INVISIBLE);
                        new_message_next.setVisibility(View.VISIBLE);
                    }
                }

            }
            return view;
        }


    }

}
